// packagBuy/pages/specialColumn/specialColumn.js
var util = require('../../../utils/util')   //引入配置公共请求接口
import {
  querySkuByPage,
  queryPageSet
} from '../../../config/config'
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    footprintGuid: '', //足迹全局id
    specialColumnId: null,
    imgUrl: getApp().globalData.imgUrl,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    titleHeight: '',
    listData: [],
    queryPageSetData: {}, // 页面设置数据
    /**-合伙人--*/
    isShowPartnerAll: 1,
    showPartnerCodePop: false,
    authorizationPopup: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getLogin((re)=>{
      if(!re){
        this.setData({
          authorizationPopup: true
        })
      }
    })
    let that = this
    wx.getSystemInfo({
      success (res) {
        let statusBarHeight = res.statusBarHeight
        let headerHeight = statusBarHeight + 50
        that.setData({
          titleHeight: statusBarHeight,
          headerHeight: headerHeight
        })
      }
    })
    let addrCode = wx.getStorageSync('addrCode')
    that.setData({
      addrCode: addrCode
    })
    const reloadListFlag = true
    if (options.id && !options.hid) {
      let callFrom = 'special_column_'+options.id
      that.setData({
        specialColumnId: options.id,
        callFrom: callFrom
      })
      that.gettypeData(reloadListFlag, that.data.callFrom) // 获取分类
      that.getqueryPageSetData(that.data.specialColumnId) // 获取页面设置元素
    }
      if (options.scene || options.hid) {
        var scene = options.hid ? options.hid : decodeURIComponent(options.scene) //参数二维码传递过来的参数
      if (options.scene) {
        app.getShareCode(scene, (res) => {
          let callFrom = 'special_column_'+res.id
          that.setData({
            // temporaryAddressData: temporaryAddressData,
            specialColumnId: res.id,
            callFrom: callFrom
          })
          var stInFun = setInterval(() => {
            if (wx.getStorageSync('getLocation') === 'N') {
              that.setData({
                showSetting: true
              })
            }
            if (wx.getStorageSync('loginMsg').token && wx.getStorageSync('addrCode').provinceCode) {
              that.setData({
                showSetting: false
              })
              clearInterval(stInFun)
              if (!wx.getStorageSync('loginMsg').loginState) {
                if (wx.getUserProfile) {
                  app.getUserProfile(function(){
                    that.setData({
                      getPhoneNumberData:{
                        isShowGetPhoneNumber:true
                      }
                    })
                  })
                } else {
                  if(!wx.getStorageSync('loginMsg').bindingPhone){
                    that.setData({
                      getPhoneNumberData:{
                        isShowGetPhoneNumber:true
                      }
                    })
                  }
                }
              }
              app.bindingPartner(res.userId)
              that.gettypeData(reloadListFlag, that.data.callFrom) // 获取分类
              that.getqueryPageSetData(that.data.specialColumnId) // 获取页面设置元素
            }
          }, 500)
        })
      } else if (options.hid) {
        let callFrom = 'special_column_'+options.id
        that.setData({
          specialColumnId: options.id,
          callFrom: callFrom
        })
        var shareData={userId:options.hid}
        wx.setStorageSync('shearData', shareData)

        // that.setData({
        //   specialColumnId: options.id
        // })
        var stInFun = setInterval(() => {
          if (wx.getStorageSync('getLocation') === 'N') {
            that.setData({
              showSetting: true
            })
          }
          if (wx.getStorageSync('loginMsg').token && wx.getStorageSync('addrCode').provinceCode) {
            that.setData({
              showSetting: false
            })
            clearInterval(stInFun)
            if (!wx.getStorageSync('loginMsg').loginState) {
              if (wx.getUserProfile) {
                app.getUserProfile(function(){
                  that.setData({
                    getPhoneNumberData:{
                      isShowGetPhoneNumber:true
                    }
                  })
                })
              } else {
                if(!wx.getStorageSync('loginMsg').bindingPhone){
                  that.setData({
                    getPhoneNumberData:{
                      isShowGetPhoneNumber:true
                    }
                  })
                } else {
                  callback(true)  //低版本微信绑过电话视为已登录
                }
              }
            }
            app.bindingPartner(options.hid)
            that.gettypeData(reloadListFlag, that.data.callFrom) // 获取分类
            that.getqueryPageSetData(that.data.specialColumnId) // 获取页面设置元素
          }
        }, 500)
      }
      }
      // var stInFun = setInterval(() => {
      //   if (wx.getStorageSync('loginMsg').token && wx.getStorageSync('addrCode').provinceCode) {
      //     clearInterval(stInFun)
      //     if (options.hid) {
      //       console.log('合伙人分享')
      //       var hid = options.hid
      //       app.bindingPartner(hid)
      //     }
      //     that.gettypeData(reloadListFlag, callFrom)
      //     that.getqueryPageSetData(options.id)
      //   }
      // }, 1000)
    
  },
  listSelect(e) {
    this.setData({
      listCur: e.currentTarget.dataset.id,
      cat: e.currentTarget.dataset.id,
      page: 1,
      rows: 10,
      nomoreData: false,
      listData: []
    })
    this.getquerySkuByPage(e.currentTarget.dataset.id, '', this.data.callFrom)
  },
  // 获取页面设置元素
  getqueryPageSetData(id) {
    let that = this
    let queryPageSetUrl = queryPageSet +`/${id}`
    app.reqFun.requestJson('GET', queryPageSetUrl, '', (res) => {
      if (res.data.success == 1) {
        that.setData({
          queryPageSetData:res.data.data
        })
      }
    })
  },
  // 精选专区
  getquerySkuByPage(cat, reloadListFlag, callFrom) {
    let that = this
    let option = {}
    option.page = that.data.page
    option.rows = that.data.rows
    option.callFrom = callFrom
    option.provinceCode = that.data.addrCode.provinceCode
    option.cat = cat
    app.reqFun.requestJson('POST', querySkuByPage, option, (res) => {
      if (res.data.success == 1) {
        let records = res.data.data.solrProductDtos.records
        if (records.length < that.data.rows) {
          that.setData({
            nomoreData: true
          })
        }
        if (reloadListFlag) {
          that.setData({
            listData: records
          })
        } else {
          that.setData({
            listData: that.data.listData.concat(records)
          })
        }
      }
    })
  },
  gettypeData(reloadListFlag, callFrom) {
    let that = this
    let option = {}
    option.page = that.data.page
    option.rows = that.data.rows
    option.callFrom = callFrom
    option.provinceCode = that.data.addrCode.provinceCode
    app.reqFun.requestJson('POST', querySkuByPage, option, (res) => {
      if (res.data.success == 1) {
        let productCategoryFacetDtos = res.data.data.productCategoryFacetDtos
        let type = {
          id: '',
          name: '全部'
        }
        productCategoryFacetDtos.unshift(type)
        that.setData({
          typeData: productCategoryFacetDtos,
          listCur: productCategoryFacetDtos[0].id,
          cat: productCategoryFacetDtos[0].id,
        })
        that.getquerySkuByPage(productCategoryFacetDtos[0].id, reloadListFlag, callFrom)
      }
    })
  },
  goreBack: function (e) { //返回
    wx.navigateBack({
      delta: 1
    })
  },
  goHome: function (e) { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  // 跳转商品详情
  goGoodsInfo(option) {
    let skuid = option.currentTarget.dataset.skuid
    console.log('skuid', skuid)
    wx.navigateTo({
        url: '/packageShop/pages/goodsInfo/goodsInfo?id=' + skuid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    let query = wx.createSelectorQuery();
    query.select('.fixed_top').boundingClientRect(rect=>{
      let clientHeight = rect.height;
      // let clientWidth = rect.width;
      // let ratio = 750 / clientWidth;
      // let height = clientHeight * ratio;
      that.setData({
        topHeight: clientHeight
      })
    }).exec();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      footprintGuid: util.guid()
    })
    setTimeout(()=>{
      this.setData({
        partnerInfo:getApp().globalData.partnerInfo
      })
      },500)
    console.log('专栏id', this.data.specialColumnId)
    //保存访问足迹
    app.saveAccessFootprint(this.data.footprintGuid, 'specialColumn', this.data.specialColumnId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

 /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.nomoreData) {
      this.setData({
        page: this.data.page + 1
      })
      this.getquerySkuByPage(this.data.cat, '', this.data.callFrom)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let title = this.data.queryPageSetData.name
    const path = 'packagBuy/pages/specialColumn/specialColumn?hid=' + wx.getStorageSync('loginMsg').userId
    + '&id=' + this.data.specialColumnId
    const shareInfo = {
      title: title,
      desc: '商品专栏-' + title,
      path: path
    }
    return shareInfo
  },
  //展开合伙人信息
  showPartnerAll: function () {
    let isShowPartnerAll;
    if (this.data.isShowPartnerAll == 1) {
      isShowPartnerAll = 0;
    } else {
      isShowPartnerAll = 1;
    }
    this.setData({
      isShowPartnerAll: isShowPartnerAll,
    })
  },
  //展示合伙人二维码
  partnerCode: function (e) {
    // this.setData({
    //   showPartnerCodePop: true,
    // })
    let codeImg=e.target.dataset.url
    wx.previewImage({
      urls:[codeImg],
      current: codeImg, // 当前显示图片的http链接
     })
  },
  //关闭合伙人二维码
  closeCodePop: function () {
    console.log("3253456678dsgudkdkgfksfvkjdsvbjkdsvbvkj")
    this.setData({
      showPartnerCodePop: false,
    })
  },
  //拨打合伙人电话
  partnerPhone: function () {
    let that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.partnerInfo.partnerPhone,
    })
  }
})